package com.huntech.data.util;

import lombok.Data;

import java.util.List;

@Data
public class Result<T> {

    /*错误码*/
    private Integer code;
    /*提示信息*/
    private String message;
    /*具体的内容*/
    private T data;
    /*具体的内容集合个数*/
    private Integer total;
    /*具体的内容集合*/
    private List<T> rows;
    /*成功true  失败false*/
    private Boolean rel;

    public Result(int code, String msg, T result) {
        this.code = code;
        this.message = msg;
        this.data = result;
    }

    public Result(int code, String msg) {
        this.code = code;
        this.message = msg;
        this.rel = true;

    }

    public Result(int code, String msg, List<T> rows) {
        this.code = code;
        this.message = msg;
        this.rows = rows;
    }

    public Result(int code, String msg, Integer total) {
        this.code = code;
        this.message = msg;
        this.total = total;
    }


    public Result() {

    }
    /**
     * 成功
     */
    public static final int SUCCESS = 200;

    /**
     * 失败
     */
    public static final int FAIL = 500;

    private static <T> Result<T> restResult(T data, int code, String msg) {
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setData(data);
        r.setMessage(msg);
        return r;
    }

    public static <T> Result<T> ok() {
        return restResult(null, SUCCESS, "操作成功");
    }

    public static <T> Result<T> ok(T data) {
        return restResult(data, SUCCESS, "操作成功");
    }

    public static <T> Result<T> ok(String msg) {
        return restResult(null, SUCCESS, msg);
    }

    public static <T> Result<T> ok(String msg, T data) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> Result<T> fail() {
        return restResult(null, FAIL, "操作失败");
    }

    public static <T> Result<T> fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> Result<T> fail(T data) {
        return restResult(data, FAIL, "操作失败");
    }

    public static <T> Result<T> fail(String msg, T data) {
        return restResult(data, FAIL, msg);
    }

    public static <T> Result<T> fail(int code, T data) {
        return restResult(data, code, "操作失败");
    }

    public static <T> Result<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

}